<?php
namespace app\interfaces\controller;

use app\interfaces\model\Promotion as PromotionModel;
use app\interfaces\model\ShopGoods as ShopGoodsModel;
use think\Request;
use think\Exception;
use think\Response;

class Promotion
{

    /*
     * 用户端 - 促销活动列表
     */
    public function pro_list()
    {

        $model = new PromotionModel();
        $result = $model->where(['status' => '1'])
            ->field('id,title,content,goods_id,picUrl,create_time')
            ->order(['sort' => 'desc', 'create_time' => 'desc'])
            ->paginate(4);

        // foreach ($result as &$value) {
        //     $value['content'] = strip_tags($value['content']);
        //
        // }


        foreach ($result as $key => &$value) {
            $value['goods'] = ShopGoodsModel::where(['id' => ['in', $value['goods_id']]])->field('id,thumb_img')->limit('3')->select();
            $value['create_time'] = date('Y-m-d', $value['create_time']);
        }

        return ['code' => 200, 'message' => '成功', 'data' => $result];


    }

    /*
     * 用户端 - 活动提及商品
     * @param  string $goods_id 唯一标示
     */
    public function pro_goods()
    {

        // 获取参数
        $goods_id = Request::instance()->param('id');

        if (empty($goods_id)) throw new Exception('not Empty');
        $content = '<style> img {vertical-align:middle; width:100%; max-width:100%;} </style>';
        $content .= '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">';
        $content .= db('promotion')->where(['id' => ['in', $goods_id]])->value('content');
        return Response::create($content, 'html');


    }

}
